home *** CD-ROM | disk | FTP | other *** search
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Random;
- import java.util.Vector;
-
- public class CoctailBar {
- Hashtable coctails;
- Random random;
-
- public CoctailBar() {
- this(10);
- }
-
- public CoctailBar(int barSize) {
- this.coctails = new Hashtable(barSize);
- this.random = new Random();
- }
-
- public void addCoctail(Coctail coctail) {
- this.coctails.put(coctail.getName(), coctail);
- }
-
- public void addCoctail(String name, String ingredients, String recipe) {
- Coctail c = new Coctail(name, ingredients, recipe);
- this.coctails.put(name, c);
- }
-
- public Coctail getCoctail(String name) {
- name = name.toUpperCase();
- Enumeration enum = this.coctails.keys();
- String coctail = null;
-
- while(enum.hasMoreElements()) {
- coctail = (String)enum.nextElement();
- if (name.equals(coctail.toUpperCase())) {
- break;
- }
- }
-
- return (Coctail)this.coctails.get(coctail);
- }
-
- public Coctail getRandomCoctail() {
- Enumeration enum = this.coctails.elements();
- int i = Math.abs(this.random.nextInt() % this.coctails.size());
- int j = 0;
-
- Coctail coctail;
- do {
- coctail = (Coctail)enum.nextElement();
- } while(j++ < i && enum.hasMoreElements());
-
- return coctail;
- }
-
- public Coctail setScore(String coctailName, int score) {
- Coctail coctail = this.getCoctail(coctailName);
- coctail.setScore(score);
- return coctail;
- }
-
- public String[] searchCoctail(String byName, String byIngredients) {
- Vector coctails = null;
- if (byName.length() != 0) {
- coctails = this.searchCoctailByName(byName.toUpperCase());
- } else if (byIngredients.length() != 0) {
- coctails = this.searchCoctailByIngredients(byIngredients.toUpperCase());
- }
-
- String[] coctailList = new String[coctails.size()];
- coctails.copyInto(coctailList);
- return coctailList;
- }
-
- private Vector searchCoctailByName(String name) {
- Enumeration enum = this.coctails.keys();
- String key = null;
- Vector coctailList = new Vector();
-
- while(enum.hasMoreElements()) {
- key = (String)enum.nextElement();
- key = key.toUpperCase();
- if (key.indexOf(name) != -1) {
- coctailList.addElement(key);
- }
- }
-
- return coctailList;
- }
-
- private Vector searchCoctailByIngredients(String ingredients) {
- Enumeration enum = this.coctails.elements();
- String key = null;
- Vector coctailList = new Vector();
-
- while(enum.hasMoreElements()) {
- Coctail coctail = (Coctail)enum.nextElement();
- String coctailIngredients = coctail.getIngredients().toUpperCase();
- if (coctailIngredients.indexOf(ingredients) != -1) {
- coctailList.addElement(coctail.getName());
- }
- }
-
- return coctailList;
- }
-
- public String[] getCoctailList() {
- Enumeration enum = this.coctails.keys();
- String[] coctailList = new String[this.coctails.size()];
-
- for(int i = 0; enum.hasMoreElements(); coctailList[i++] = (String)enum.nextElement()) {
- }
-
- return coctailList;
- }
- }
-